var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text_font: "",
    image: "",
    bgcolor: "",
    isHidden: false,
    nickName: "",
    avatarUrl: "",
    name: "",
    username: "",
    identity: "",
    isQuit: false,
    fontColor: "#303030",
    backImage: "/images/16.png",
    canIuse: wx.canIUse('button.open-type.getUserInfo')
  },

  coursetableTap: function() {
    wx.navigateTo({
      url: '../coursetable/coursetable',
    })
  },

  recordTap: function() {
    wx.navigateTo({
      url: '../record/record',
    })
  },

  managementTap: function() {
    var identity = app.globalData.identity
    if (identity === "student") {
      wx.navigateTo({
        url: '../record/record',
      })
    } else {
      wx.navigateTo({
        url: '../management/management',
      })
    }
  },

  signTap:function(){
    wx.navigateTo({
      url: '../signIn/signIn',
    })
  },

  isQuitTap: function() {
    var that = this
    var isQuit = this.data.isQuit
    this.setData({
      fontColor: "#c0c0c0",
      backImage: "/images/16_1.png"
    })

    if (isQuit === false) {
      isQuit = !isQuit
      that.setData({
        isQuit: isQuit,
      })
    } else {
      isQuit = !isQuit
      that.setData({
        isQuit: isQuit,
        fontColor: "#303030",
        backImage: "#/images/16.png"
      })
    }
  },

  QuitTap: function() {
    wx.clearStorage({
      success: function() {
        wx.showModal({
          title: '提示',
          content: '退出登录成功',
          showCancel: false
        })
        wx.redirectTo({
          url: '../login/login',
        })
      },

      fail: function() {
        wx.showModal({
          title: '提示',
          content: '退出登录失败，请重试',
          showCancel: false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var isHidden = this.data.isHidden
    var name = app.globalData.name
    var identity = app.globalData.identity
    var isAdmin = app.globalData.isAdmin
    var username = wx.getStorageSync("username")
    var password = wx.getStorageSync("password")
    var isRequest = wx.getStorageSync("isRequest")
    if (app.globalData.userInfo) {
      that.setData({
        nickName: app.globalData.nickName,
        avatarUrl: app.globalData.avatarUrl
      })
    } else if (that.data.canIuse) {
      app.userInfoReadyCallback = res => {
        that.setData({
          nickName: app.globalData.nickName,
          avatarUrl:app.globalData.avatarUrl
        })
      }
    }
    
    if (identity === "student") {
      identity = "学生"
      that.setData({
        bgcolor: "skyblue",
        text_font: "签到记录",
        image: "/images/5.svg",
        identity: identity,
        name: name,
        username: username
      })
    } else {
      identity = "教师"
      that.setData({
        bgcolor: "#ce9be6",
        text_font: "学生管理",
        image: "/images/15.svg",
        identity: identity,
        name: name,
        username: username
      })
    }


    if (isAdmin === "1") {
      that.setData({
        isHidden: !that.data.isHidden,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})